<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../../top.jsp"%>
<style type="text/css">
*{
margin:0;
padding:0;
}
.usertdlabel{
width: 15%;
text-align: right !important; 
}
.usertdval{
width: 30%;
}
.inputVal{
margin-top: 4%;
 }
tr{
width: 100%;
} 
input{
margin-left:4%;
}
.lineName{
	background-color: silver;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑用户信息</title>
</head>
<body>	
		<input type="hidden" name="USER_ID" id="user_id" value="${pd.USER_ID }"/>
		<div id="zhongxin">
		<table id="table_report" class="table table-striped table-bordered table-hover">
			
			<c:if test="${fx != 'head'}">
			<c:if test="${pd.ROLE_ID == '1'}">
			<input name="ROLE_ID" id="role_id" value="1" type="hidden" />
			</c:if>
			</c:if>
			
			<c:if test="${fx == 'head'}">
				<input name="ROLE_ID" id="role_id" value="${pd.ROLE_ID }" type="hidden" />
			</c:if>
			<h2 style="font-weight:bold;" id="titleH1">新增用户</h2>
			<tr>
				<td class="usertdlabel">编号:</td>
				<td class="usertdval"><input type="text" name="NUMBER"  class="inputVal" id="NUMBER" value="${pd.NUMBER }" maxlength="32" placeholder="这里输入编号" title="编号" onblur="hasN('${pd.USERNAME }')"/></td>
				<td class="usertdlabel">用户名:</td>
				<td class="usertdval"><input type="text" name="USERNAME" class="inputVal" id="loginname" value="${pd.USERNAME }" maxlength="32" placeholder="这里输入用户名" title="用户名" onblur="hasU()" /></td>
			
			</tr>
			<%-- <tr>
				<td class="usertdlabel">用户名:</td>
				<td class="usertdval"><input type="text" name="USERNAME" class="inputVal" id="loginname" value="${pd.USERNAME }" maxlength="32" placeholder="这里输入用户名" title="用户名" onblur="hasU()" /></td>
			
			</tr> --%>
			<tr>
				<td class="usertdlabel">密码:</td>
				<td class="usertdval"><input type="password" name="PASSWORD"   class="inputVal" id="password"  maxlength="32" placeholder="输入密码" title="密码"/></td>
				<td class="usertdlabel">确认密码:</td>
				<td class="usertdval"><input type="password" name="chkpwd"   class="inputVal" id="chkpwd"  maxlength="32" placeholder="确认密码" title="确认密码" /></td>
			</tr>
			<!-- <tr>
				<td class="usertdlabel">确认密码:</td>
				<td class="usertdval"><input type="password" name="chkpwd"   class="inputVal" id="chkpwd"  maxlength="32" placeholder="确认密码" title="确认密码" /></td>
			
			</tr> -->
			<tr>
				<td class="usertdlabel">姓名:</td>
				<td class="usertdval"><input type="text" name="NAME" id="name"  class="inputVal"  value="${pd.NAME }"  maxlength="32" placeholder="这里输入姓名" title="姓名"/></td>
				<td class="usertdlabel">平台角色:</td>
				<td class="lineName usertdval">
					<select style="margin-left:4%" id="platformRole">
						<!-- <option></option>
						<option>123</option>
						<option>123</option>
						<option>123</option>
						<option>123</option> -->
					</select>
				</td>
			</tr>
			<%-- <tr>
				<td class="usertdlabel">姓名:</td>
				<td class="usertdval"><input type="text" name="NAME" id="name"  class="inputVal"  value="${pd.NAME }"  maxlength="32" placeholder="这里输入姓名" title="姓名"/></td>
			
			</tr> --%>
			<tr>
				<td class="usertdlabel">手机号:</td>
				<td class="usertdval"><input type="number" name="PHONE" id="PHONE"  class="inputVal"  value="${pd.PHONE }"  maxlength="32" placeholder="这里输入手机号" title="手机号"/></td>
				<td class="usertdlabel">电子邮件:</td>
				<td class="usertdval"><input type="email" name="EMAIL" id="EMAIL"   class="inputVal" value="${pd.EMAIL }" maxlength="32" placeholder="这里输入邮箱" title="邮箱" onblur="hasE('${pd.USERNAME }')"/></td>
			
			</tr>
			<%-- <tr>
				<td class="usertdlabel">电子邮件:</td>
				<td class="usertdval"><input type="email" name="EMAIL" id="EMAIL"   class="inputVal" value="${pd.EMAIL }" maxlength="32" placeholder="这里输入邮箱" title="邮箱" onblur="hasE('${pd.USERNAME }')"/></td>
			
			</tr> --%>
			<tr>
				<td class="usertdlabel">备注:</td>
				<td class="usertdval" colspan="3" style="margin-left:2%"><input type="text" name="BZ" id="BZ" value="${pd.BZ }"  class="inputVal" placeholder="这里输入备注" maxlength="64" title="备注"/></td>
				
			</tr>
		</table>
		</div>
		
		<!-- <div id="subSystem" style="width:100%;">
			<h1 style="font-weight:bold;">子系统平台</h1>
		</div> -->
		<h2 style="font-weight:bold;" id="titleName">线路分配</h2>
		<div id="subLine" style="width:100%; display: inline;">
			<!-- <table class="table table-bordered" style="text-align: center;">
				<tr>
					<td class="lineName usertdlabel" >12号线</td>
					<td class="usertdval">
						<select>
							<option>1</option>
							<option>1</option>
							<option>1</option>
						</select>
					</td>
					<td class="lineName usertdlabel" >11号线</td>
					<td class="usertdval">
						<select>
							<option value="1">1</option>
							<option>1</option>
							<option>1</option>
						</select>
					</td>
				</tr>
			</table> -->
		</div>
		<div id="buttonDiv" style="text-align: center;margin-bottom: 5%;">
			<a class="btn btn-mini btn-primary" onclick="save();">保存</a>
					<a class="btn btn-mini btn-danger" onclick="top.Dialog.close();">取消</a>
		</div>
</body>

		
<script type="text/javascript">
	$(document).ready(function(){
		top.hangge();
		if($("#user_id").val()!=""){
			$("#titleH1").html("修改用户");
			$("#loginname").attr("disabled","disabled");
			$("#loginname").css("color","gray");
			$("#NUMBER").attr("disabled","disabled");
			$("#NUMBER").css("color","gray");
		}
		initAjax();
		huixian();
		
		
	});
	var msg ='${msg}';
	var selectids = new Array();
	var menuids = new Array();
	//获取子系统信息和除了系统管理员角色
	function initAjax(){
		$.ajax({
			url:'<%=basePath%>user/getsubSysData.do',
			dataType:'json',
			async:false,
			success:function(data){

				var roles = data.roles;//平台门户系统角色
				var subLines = data.subLines;//子线路系统
				var lineRoles = data.lineRoles;//子线路系统角色
				//var subsyses = data.subsyses;
				var rolehtml = "";
				var subsyshtml = "";
				var optionhtml="<option value=''></option>";
				var optionForLineRole = "<option value=''></option>";
				
				//平台门户系统角色下拉框
				for(var i = 0;i<roles.length;i++){
					optionhtml+="<option value='"+roles[i].role_ID+"'>"+roles[i].role_NAME+"</option>";
				}
				$("#platformRole").append(optionhtml);
				//登录系统菜单列表
				/*for(var i = 0;i<subsyses.length;i++){
					subsyshtml ="<div style='width:55%;float:left;margin-top:1.5%;'><span>"+subsyses[i].MENU_NAME+":</span></div>";
					rolehtml ="<div style='width:42%;float:left;'><select id='"+subsyses[i].MENU_ID+"' style='width:90%;'></select></div>";
					$("#subSystem h1").after("<div style='text-align:right;width:99%;'>"+subsyshtml+rolehtml+"</div>");
					
					//给下拉框赋值
					$("#"+subsyses[i].MENU_ID).append(optionhtml);
					//系统id放到数组里，保存用
					selectids.push(subsyses[i].MENU_ID);
				} */
				
				//子线路系统角色下拉框
				/* for(var i = 0;i<lineRoles.length;i++){
					optionForLineRole+="<option value='"+lineRoles[i].roleCode+"'>"+lineRoles[i].roleName+"</option>";
				} */
				//子线路系统菜单列表
				var table = $('<table class="table table-striped table-bordered table-hover" style="text-align: center;">');
				var tr = null;
				var roleOption = null;
				for(var i = 0;i<subLines.length;i++){
					if(i%2==0){
						tr = $('<tr>');
					}
					tr.append($('<td class=" usertdlabel" >').append(subLines[i].lineName));
					//获取线路对应的角色
					roleOption = $('<select id='+subLines[i].lineId+'><option>');
					for(m=0;m<lineRoles.length;m++){
						roleOption.append($('<option value='+lineRoles[m].role_ID+'>').append(lineRoles[m].role_NAME));
					}
					tr.append($('<td class="usertdval">').append(roleOption));
					table.append(tr);
					
					/* subsyshtml ="<div style='width:55%;float:left;margin-top:1.5%;'><span>"+subLines[i].lineName+":</span></div>";
					rolehtml ="<div style='width:42%;float:left;'><select id='"+subLines[i].lineId+"' style='width:90%;'></select></div>";
					$("#subSystem h1").after("<div style='text-align:right;width:99%;'>"+subsyshtml+rolehtml+"</div>");
					
					//给下拉框赋值
					$("#"+subLines[i].lineId).append(optionForLineRole);*/
					//系统id放到数组里，保存用
					selectids.push(subLines[i].lineId); 
					menuids.push(subLines[i].menuId);
				}
				$("#subLine").html(table);
				
				
			}
		});
	}
	
	
	function huixian(){
		$.ajax({
			url:'<%=basePath%>user/showUserSysRoles.do',
			dataType:'json',
			data:{USER_ID:$("#user_id").val()},
			success:function(data){
				if(data.msg=='ok'){
				var d =data.userSysRoles;
				for(var i=0;i<d.length;i++){
					$("#"+d[i].MENU_ID).val(d[i].ROLEID);
				}
				}
			}
		})
	}
	
	//保存
	function save(){
		if($("#loginname").val()=="" || $("#loginname").val()=="此用户名已存在!"){
			
			$("#loginname").tips({
				side:3,
	            msg:'输入用户名',
	            bg:'#AE81FF',
	            time:2
	        });
			
			$("#loginname").focus();
			$("#loginname").val('');
			$("#loginname").css("background-color","white");
			return false;
		}else{
			$("#loginname").val(jQuery.trim($('#loginname').val()));
		}
		
		if($("#NUMBER").val()==""){
			
			$("#NUMBER").tips({
				side:3,
	            msg:'输入编号',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#NUMBER").focus();
			return false;
		}else{
			$("#NUMBER").val($.trim($("#NUMBER").val()));
		}
		
		if($("#user_id").val()=="" && $("#password").val()==""){
			
			$("#password").tips({
				side:3,
	            msg:'输入密码',
	            bg:'#AE81FF',
	            time:2
	        });
			
			$("#password").focus();
			return false;
		}
		if($("#password").val()!=$("#chkpwd").val()){
			
			$("#chkpwd").tips({
				side:3,
	            msg:'两次密码不相同',
	            bg:'#AE81FF',
	            time:3
	        });
			
			$("#chkpwd").focus();
			return false;
		}
		if($("#name").val()==""){
			
			$("#name").tips({
				side:3,
	            msg:'输入姓名',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#name").focus();
			return false;
		}
		
		var myreg = /^(((13[0-9]{1})|159)+\d{8})$/;
		if($("#PHONE").val()==""){
			
			$("#PHONE").tips({
				side:3,
	            msg:'输入手机号',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#PHONE").focus();
			return false;
		}else if($("#PHONE").val().length != 11 && !myreg.test($("#PHONE").val())){
			$("#PHONE").tips({
				side:3,
	            msg:'手机号格式不正确',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#PHONE").focus();
			return false;
		}
		
		if($("#EMAIL").val()==""){
			
			$("#EMAIL").tips({
				side:3,
	            msg:'输入邮箱',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#EMAIL").focus();
			return false;
		}else if(!ismail($("#EMAIL").val())){
			$("#EMAIL").tips({
				side:3,
	            msg:'邮箱格式不正确',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#EMAIL").focus();
			return false;
		}	
			$("#zhongxin").hide();
			$("#subSystem").hide();
			$("#subLine").hide();
			$("#titleName").hide();
			$("#buttonDiv").hide();
			
			
			
			var url ='';
			
			if('saveU'==msg){
				 url ='<%=basePath%>user/saveSystemRole.do';
			}else{
				 url ='<%=basePath%>user/updateSystemRole.do';
			}
			
			var mapList = new Array();
			for(var i = 0;i<selectids.length;i++){
				var lineId =selectids[i];
				var menuId = menuids[i];
				var roleId =$("#"+lineId).val();
				var menu_role =new MenuRole(lineId,roleId,lineId,menuId);
				mapList.push(menu_role);
			}
			var platformRole = $("#platformRole").val();
			if(platformRole != null && platformRole.length > 0)
				{
					var menu_role =new MenuRole("platformRole",platformRole,null,"2");//默认平台系统的菜单Id为2
					mapList.push(menu_role);
				}
			var userParam = new UserParam(mapList);
			$.ajax({
				 url:url,
				 type:'POST',
				 contentType:'application/json;charset=UTF-8',
				 data:JSON.stringify(userParam),
				 success:function(callback){
						 bootbox.dialog(callback.message, [ {
								"label" : "关闭",
								"class" : "btn-small btn-success",
								"callback" : function() {
									top.Dialog.close();
								}
							} ]);
				},
				error:function(){
					bootbox.alert("系统异常");
				}
			});
	}
	
	//菜单角色对象
	function MenuRole(sysId,roleId,lineId,menuId){
		this.sysId = sysId;
		this.roleId = roleId;
		this.lineId = lineId;
		this.menuId = menuId;
	}
	
	
	//用户对象
	function UserParam(mapList){
		this.USER_ID =$("#user_id").val();
		this.USERNAME = $("#loginname").val();
		this.NUMBER = $("#NUMBER").val();
		this.PASSWORD = $("#password").val();
		this.NAME = $("#name").val();
		this.PHONE = $("#PHONE").val();
		this.EMAIL = $("#EMAIL").val();
		this.BZ = $("#BZ").val();
		this.mapList = mapList;
	}
	
	function ismail(mail){
		return(new RegExp(/^(?:[a-zA-Z0-9]+[_\-\+\.]?)*[a-zA-Z0-9]+@(?:([a-zA-Z0-9]+[_\-]?)*[a-zA-Z0-9]+\.)+([a-zA-Z]{2,})+$/).test(mail));
		}
	
	
	
	
	//判断用户名是否存在
	function hasU(){
		var USERNAME = $.trim($("#loginname").val());
		$.ajax({
			type: "POST",
			url: '<%=basePath%>user/hasU.do',
	    	data: {USERNAME:USERNAME,tm:new Date().getTime()},
			dataType:'json',
			cache: false,
			success: function(data){
				 if("success" != data.result){
					 $("#loginname").tips({
							side:3,
				            msg:'此用户名已存在',
				            bg:'#AE81FF',
				            time:3
				        });
						setTimeout("$('#loginname').val('')",2000);
				 }
			}
		});
	}
	//判断邮箱是否存在
	function hasE(USERNAME){
		var EMAIL = $.trim($("#EMAIL").val());
		$.ajax({
			type: "POST",
			url: '<%=basePath%>user/hasE.do',
	    	data: {EMAIL:EMAIL,USERNAME:USERNAME,tm:new Date().getTime()},
			dataType:'json',
			cache: false,
			success: function(data){
				 if("success" != data.result){
					 $("#EMAIL").tips({
							side:3,
				            msg:'邮箱已存在',
				            bg:'#AE81FF',
				            time:3
				        });
					setTimeout("$('#EMAIL').val('')",2000);
				 }
			}
		});
	}
	
	//判断编号是否存在
	function hasN(USERNAME){
		var NUMBER = $.trim($("#NUMBER").val());
		$.ajax({
			type: "POST",
			url: '<%=basePath%>user/hasN.do',
	    	data: {NUMBER:NUMBER,USERNAME:USERNAME,tm:new Date().getTime()},
			dataType:'json',
			cache: false,
			success: function(data){
				 if("success" != data.result){
					 $("#NUMBER").tips({
							side:3,
				            msg:'编号已存在',
				            bg:'#AE81FF',
				            time:3
				        });
					setTimeout("$('#NUMBER').val('')",2000);
				 }
			}
		});
	}
	
</script>
</html>